<?php
/**
 * Module.php
 * 
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/** base class */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Naneau_Controller_Plugin_Module
 * 
 * a module-based plugin dispatcher for plugins
 *
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Plugin_Module extends Zend_Controller_Plugin_Abstract {

    /**
     * the plugins:
     * 
     * $this->_plugins['moduleName'][] = plugin1;
     * $this->_plugins['moduleName'][] = plugin2;
     * 
     * @var array
     */
    private $_plugins = array();

    /**
     * add a plugin
     *
     * @param string $moduleName
     * @param Zend_Controller_Plugin_Abstract $plugin
     * @return void
     */
    public function addPlugin($moduleName, $plugin) {
        $this->_plugins[$moduleName][] = $plugin;

    }

    /**
     * execute module based hook
     *
     * @param string $hook
     * @param Zend_Controller_Request_Abstract $request
     * @return void
     */
    private function executeHook($hook, $request = null) {
        $moduleName = $this->getRequest()->getModuleName();
        //the module name

        if (isset($this->_plugins[$moduleName])) {
            //there are plugins registered for this module;
            foreach ($this->_plugins[$moduleName] as $plugin) {
                if (method_exists($plugin, $hook)) {
                    
                    $plugin->setRequest($this->getRequest());
                    $plugin->setResponse($this->getResponse());
                    //set up request/response (plugin isn't registered with the FC)

                    call_user_func(array($plugin, $hook), $request);
                    //execute the hook on the plugin
                }
            }
        }
    }

    /**
     * routeShutdown hook
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request) {
        $this->executeHook('routeShutdown', $request);
    }

    /**
     * dispatchLoopStartup hook
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
        $this->executeHook('dispatchLoopStartup', $request);
    }

    /**
     * preDispatch hook
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $this->executeHook('preDispatch', $request);
    }

    /**
     * postDispatch hook
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request) {
        $this->executeHook('postDispatch', $request);
    }

    /**
     * dispatchLoopShutdown hook
     *
     */
    public function dispatchLoopShutdown() {
        $this->executeHook('dispatchLoopShutdown', $request);
    }
}